package com.oracle.malldemo.dao.impl;

import com.oracle.malldemo.dao.OrderMainDao;
import com.oracle.malldemo.domain.Items;
import com.oracle.malldemo.domain.OrderMain;
import com.oracle.malldemo.util.DruidDBUtil;
import com.oracle.malldemo.util.MyUtil;
import com.oracle.malldemo.view.Cart;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderMainDaoImpl implements OrderMainDao {


    @Override
    public String saveOrder(Cart cart) {
        String oid = MyUtil.generateOrderId();
        String sql = "insert into order_main values (?,?,?,?,?,?,?,?,?,?);";
        List<Object> argsList = new ArrayList<Object>();

        argsList.add(oid);
        argsList.add(cart.getUid());
        argsList.add(cart.getAddress().getRealname());
        argsList.add(cart.getAddress().getMobile());
        argsList.add(cart.getAddress().getProvince());
        argsList.add(cart.getAddress().getCity());
        argsList.add(cart.getAddress().getZone());
        argsList.add(cart.getAddress().getStreet());
        argsList.add(cart.getTotalPrice());
        argsList.add(MyUtil.dateToChineseDateFormat(new Date()));

        try{

            DruidDBUtil.startTransaction();

            //保存订单主表
            DruidDBUtil.executeUpdate(sql,argsList);
            //保存订单子表
            sql = "insert into order_sub values (?,?,?,?,?,?);";
            List<List> batchArgsList = new ArrayList<List>();
            for(Items i : cart.getGoodsList()) {
                argsList.clear();
                argsList.add(oid);
                argsList.add(i.getId());
                argsList.add(i.getName());
                argsList.add(i.getPrice());
                argsList.add(i.getBuyNum());
                argsList.add(i.getPrice()*i.getBuyNum());
                batchArgsList.add(argsList);
            }

            DruidDBUtil.executeBatch(sql,batchArgsList);
            DruidDBUtil.commit();
            DruidDBUtil.endTransaction();

            return oid;
        }catch(Exception ex){
            ex.printStackTrace();
            DruidDBUtil.rollback();
            DruidDBUtil.commit();
            DruidDBUtil.endTransaction();
            return "-1";
        }finally{
            DruidDBUtil.closeAll();
        }
    }

    @Override
    public OrderMain queryOrderMainByOid(String oid) {
        String sql = "select * from order_main where oid = ?;";
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(oid);
        ResultSet rs =null;
        OrderMain orderMain =null;
        try{
             rs = DruidDBUtil.executeQuery(sql,argsList);
             if(rs.next()){
                 orderMain = new OrderMain();
                 orderMain.setOid(rs.getString("oid"));
                 orderMain.setRealname(rs.getString("realname"));
                 orderMain.setUid(rs.getInt("uid"));
                 orderMain.setMobile(rs.getString("mobile"));
                 orderMain.setProvince(rs.getString("province"));
                 orderMain.setCity(rs.getString("city"));
                 orderMain.setZone(rs.getString("zone"));
                 orderMain.setStreet(rs.getString("street"));
                 orderMain.setTotalPrice(rs.getDouble("totalprice"));
                 orderMain.setCreateTime(rs.getString("create_time"));

             }
             return orderMain;
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    }
}
